package com.barneyx.mshinfoplatform.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.barneyx.mshinfoplatform.entity.info.article.ArticleAttrEntity;
import com.barneyx.mshinfoplatform.entity.info.article.ArticleEntity;
import com.barneyx.mshinfoplatform.exception.MshException;
import com.barneyx.mshinfoplatform.service.info.article.IArticleAttrEntityService;
import com.barneyx.mshinfoplatform.service.info.article.IArticleEntityService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Component
public class ArticleUtil {
    @Resource
    public IArticleEntityService articleService;

    @Resource
    public IArticleAttrEntityService articleAttrService;


    @Resource
    public AreaUtil areaUtil;


    @Transactional
    public boolean add(ArticleEntity article) {
        article.setArticleDesc(StrUtil.subPreGbk(HtmlUtil.cleanHtmlTag(article.getArticleAttr().getArticleContent()),  70,"..."));
        boolean save = articleService.save(article);
        article.getArticleAttr().setArticleAttrId(article.getArticleId());
        boolean save1 = articleAttrService.save(article.getArticleAttr());
        return save && save1;
    }

    public boolean removeById(String articleId) {
        return articleService.removeById(articleId);
    }

    @Transactional
    public boolean updateAll(ArticleEntity article) {
        article.setArticleDesc(StrUtil.subPreGbk(HtmlUtil.cleanHtmlTag(article.getArticleAttr().getArticleContent()),  70,"..."));
        boolean update = articleService.update(new LambdaUpdateWrapper<ArticleEntity>()
                .eq(ArticleEntity::getArticleId, article.getArticleId())
                .set(ArticleEntity::getArticleDesc, article.getArticleDesc())
                .set(ArticleEntity::getArticleTitle, article.getArticleTitle())
                .set(ArticleEntity::getArticleConver, article.getArticleConver()));
        ArticleAttrEntity articleAttr = article.getArticleAttr();
        boolean update1 = articleAttrService.update(new LambdaUpdateWrapper<ArticleAttrEntity>()
                .eq(ArticleAttrEntity::getArticleAttrId, articleAttr.getArticleAttrId())
                .set(ArticleAttrEntity::getArticleContent, articleAttr.getArticleContent())
                .set(ArticleAttrEntity::getVideo, articleAttr.getVideo())
                .set(ArticleAttrEntity::getAlbums, articleAttr.getAlbums())
        );
        return update && update1;

    }

    public Page<ArticleEntity> getPage(long current, long size, LambdaQueryWrapper<ArticleEntity> lambdaQueryWrapper) throws MshException {
//        lambdaQueryWrapper.eq(ArticleEntity::getAreaId, areaUtil.getCurrentAreaId());
        return articleService.page(new Page<>(current, size), lambdaQueryWrapper);
    }

    public ArticleEntity articleDetail(String articleId) {
        return articleService.getById(articleId);
    }

    public boolean updateClick(String articleId) {
        ArticleEntity article = articleService.getById(articleId);
        article.setArticleClick(article.getArticleClick() == null ? 0 : article.getArticleClick() + 1);
        return articleService.update(new LambdaUpdateWrapper<ArticleEntity>()
                .eq(ArticleEntity::getArticleId, article.getArticleId())
                .set(ArticleEntity::getArticleClick, article.getArticleClick()));
    }


    public ArticleEntity getDetail(String articleId) {
        ArticleEntity article = articleService.getById(articleId);
        if (article != null) article.setArticleAttr(articleAttrService.getById(articleId));
        return article;
    }
}
